#include <iostream>
#include <vector>

using namespace std;

//error
int numIslands(vector<vector<char>>& grid)
{
	int count = 0;
	const int width = grid[0].size();
	const int height = grid.size();
	for (int x = 0; x < width; x++)
	{
		for (int y = 0; y < height; y++)
		{
			char value = grid[y][x];
			if (value != '0')
			{
				if (y != 0)
				{
					char top = grid[y - 1][x];
					if (top != '0')
					{
						grid[y][x] = top;
						continue;
					}
				}
				if (x != 0)
				{
					char left = grid[y][x - 1];
					if (left != '0')
					{
						grid[y][x] = left;
						continue;
					}
				}
				grid[y][x] = '1' + count;
				count++;
			}
		}
	}
	return count ;
}

int main()
{
	vector<vector<char>> grid1
	{
		{'1', '1', '1', '1', '0'},
		{'1', '1', '0', '1', '0'},
		{'1', '1', '0', '0', '0'},
		{'0', '0', '0', '0', '0'}
	};
	vector<vector<char>> grid2
	{
		{'1', '1', '0', '0', '0'} ,
		{'1', '1', '0', '0', '0'},
		{'0', '0', '1', '0', '0'},
		{'0', '0', '0', '1', '1'}
	};
	cout << numIslands(grid1) << endl;
	return 0;
}